package com.module.overview.ui.cardHistoryDetails.provider

import android.util.Log
import android.widget.ProgressBar
import android.widget.TextView
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.common.app.data.bean.overView.HistoryDetailsModel
import com.common.app.utls.TimeUtils
import com.common.base.app.extras.doOnIOThread
import com.common.base.app.extras.doOnUiThread
import com.common.base.app.extras.no
import com.common.base.utils.SpanUtils
import com.common.base.utils.ViewUtils
import com.common.base.utils.ViewUtils.getString
import com.module.ble.data.bean.CusChartData
import com.module.ble.repo.SleepInfoRepo
import com.module.ble.repo.UserStressRepo
import com.module.ble.utils.AllCardTypeConfig
import com.module.ble.utils.BleOtherUtil
import com.module.ble.utils.getStressStateStrByValue
import com.module.ble.widget.StressBarChart
import com.module.ble.widget.StressBrokenLineChart
import com.module.overview.R

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/7/10
 *
 * 描述：睡眠压力卡片
 *
 * 修订历史：完成度100%
 *
 */
class OvPressureSleepCardProvider(time:Long,
    override val itemViewType: Int = AllCardTypeConfig.TYPE_CARD_PRESSURE_SLEEP,
    override val layoutId: Int = R.layout.overview_pressure_sleep_crad_provider
) : BaseItemProvider<HistoryDetailsModel>() {

    private var curTime = time
    private val curTimePair = TimeUtils.getTimeRangeForSleep(curTime)

    val twoHoursInMillis = 2 * 60 * 60 * 1000L//两小时

    private var sbrBedtimeStress: ProgressBar? = null
    private var sbrSleepStress : ProgressBar? = null
    private var tvBedtimeStress:TextView? = null
    private var tvSleepStress :TextView? = null

    init {
    }

    override fun convert(helper: BaseViewHolder, item: HistoryDetailsModel) {
        getNightSleepData()
    }



    override fun onViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        sbrBedtimeStress = viewHolder.getView(R.id.sbrBedtimeStress)
        sbrSleepStress = viewHolder.getView(R.id.sbrSleepStress)
        tvBedtimeStress = viewHolder.getView(R.id.tvBedtimeStress)
        tvSleepStress = viewHolder.getView(R.id.tvSleepStress)
        super.onViewHolderCreated(viewHolder, viewType)
    }


    private fun getNightSleepData(){
        doOnIOThread {
            SleepInfoRepo.instance.findByTimeRangeByNight(curTimePair.first, curTimePair.second)?.let { listIt ->
                listIt.sortedBy { it.createdTime }.isNullOrEmpty().no {
                    getStressDataBySleepBefore((listIt.firstOrNull()?.createdTime?:0)-twoHoursInMillis,listIt.firstOrNull()?.createdTime?:0)
                    getStressDataBySleepDuring(listIt.firstOrNull()?.createdTime?:0,listIt.lastOrNull()?.createdTime?:0)
                }
            }
        }
    }

    private fun getStressDataBySleepBefore(startTime:Long, endTime:Long){//睡眠之前
        if (startTime <=0 ||endTime <=0) return
        doOnIOThread {
            UserStressRepo.instance.findByTimeRange(curTimePair.first, curTimePair.second)?.let { listIt ->
                listIt.sortedBy { it.createdTime }.isNullOrEmpty().no {
                    val mAverage = if (listIt.isNotEmpty()) {//平均
                        listIt.map { it.stress }.average().toInt()
                    } else {
                        -1f
                    }

                    doOnUiThread {
                        sbrBedtimeStress?.progress = mAverage.toInt()
                        tvBedtimeStress?.let {
                            SpanUtils.create()
                                .addSection("${getString(R.string.overview_sleep_stress_hint)} ${mAverage.toInt()}")
                                .addForeColorSection("  ${getStressStateStrByValue(mAverage.toInt())}",when(mAverage.toInt()){
                                    in 0..30 -> ViewUtils.getColor(R.color.c_bcceff)
                                    in 31..60 -> ViewUtils.getColor(R.color.c_86b1ff)
                                    in 61..80 -> ViewUtils.getColor(R.color.c_5282ff)
                                    in 81..100 -> ViewUtils.getColor(R.color.c_ff8a06)
                                    else -> ViewUtils.getColor(R.color.c_bcceff)
                                })
                                .showIn(it)
                        }
                    }
                }
            }
        }
    }

    private fun getStressDataBySleepDuring(startTime:Long, endTime:Long){//睡眠中
        if (startTime <=0 ||endTime <=0) return
        doOnIOThread {
            UserStressRepo.instance.findByTimeRange(curTimePair.first, curTimePair.second)?.let { listIt ->
                listIt.sortedBy { it.createdTime }.isNullOrEmpty().no {
                    val mAverage = if (listIt.isNotEmpty()) {//平均
                        listIt.map { it.stress }.average().toInt()
                    } else {
                        -1f
                    }

                    doOnUiThread {
                        sbrSleepStress?.progress = mAverage.toInt()
                        tvSleepStress?.let {
                            SpanUtils.create()
                                .addSection("${getString(R.string.overview_sleep_stress_hint)} ${mAverage.toInt()}")
                                .addForeColorSection("  ${getStressStateStrByValue(mAverage.toInt())}",when(mAverage.toInt()){
                                    in 0..30 -> ViewUtils.getColor(R.color.c_bcceff)
                                    in 31..60 -> ViewUtils.getColor(R.color.c_86b1ff)
                                    in 61..80 -> ViewUtils.getColor(R.color.c_5282ff)
                                    in 81..100 -> ViewUtils.getColor(R.color.c_ff8a06)
                                    else -> ViewUtils.getColor(R.color.c_bcceff)
                                })
                                .showIn(it)
                        }
                    }
                }
            }
        }
    }
}